<template>
	<view class="personal-information">

		<uni-forms ref="registrationData" :rules="registrationRules" :modelValue="registrationData" labelWidth="150rpx"
			class='user-form'>
			<uni-forms-item label="医生" required name="doctor">
				<uni-data-select v-model="registrationData.doctor" :localdata="range">
				</uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="姓名">
				<view class="movetext">{{registrationData.name}}</view>
			</uni-forms-item>
			<uni-forms-item label="年龄" name="age">
				<view class="movetext">{{registrationData.age}}</view>
			</uni-forms-item>
			<uni-forms-item label="性别">
				<view class="movetext">{{registrationData.sex== 0 ? '男':'女'}}</view>
			</uni-forms-item>
			<uni-forms-item label="手机号">
				<view class="movetext">{{registrationData.mobilePhone}}</view>
			</uni-forms-item>
			<uni-forms-item label="病情" name="disease" required>
				<uni-easyinput type="textarea" v-model="registrationData.disease" placeholder="请输入病情" />
			</uni-forms-item>
		</uni-forms>
		<button class="btn" @click="submit">提交</button>
		<!-- 提示信息弹窗 -->
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentUser: this.$store.state.currentUser,
				sexs: [{
					text: '男',
					value: 0
				}, {
					text: '女',
					value: 1
				}],

				registrationData: {
					doctor: this.$store.state.currentUser.doctor || '',
					name: this.$store.state.currentUser.name || '',
					age: this.$store.state.currentUser.age || '',
					sex: this.$store.state.currentUser.sex || 0,
					mobilePhone: this.$store.state.currentUser.mobilePhone || '',
					disease: this.$store.state.currentUser.disease || '',
					doctorName: '',
					isConduct: true,
					nowPosition: 0
				},

				range: [],

				// 校验规则
				registrationRules: {
					doctor: {
						rules: [{
							required: true,
							errorMessage: '必须选择医生'
						}]
					},
					disease: {
						rules: [{
							required: true,
							errorMessage: '必须填写病情'
						}]
					},
				},
				msgType: "warn",
				messageText: ""

			}
		},
		mounted() {
			console.log(this.$store.state);
			let doctorManagement = this.$store.state.doctorManagement
			// console.log(doctorManagement);
			doctorManagement.forEach((item, index) => {
				let obj = {
					value: item.account,
					text: item.name || `医生${index}`
				}
				this.range.push(obj)
			})
			if (this.currentUser.registrationData && this.currentUser.registrationData[0].isConduct == true) {
				this.registrationData = this.currentUser.registrationData[0]
			}
		},
		methods: {
			submit() {
				this.$refs['registrationData'].validate().then(res => {
					// uni.showToast({
					// 	title: `提交成功`
					// })
					this.range.forEach(item => {
						if (item.value == this.registrationData.doctor) {
							this.registrationData.doctorName = item.text
						}
					})
					console.log(123);
					if (this.currentUser.registrationData) {
						console.log(this.currentUser.registrationData[0], '123123123123');
						if (this.currentUser.registrationData[0].nowPosition == 1) {
							// 更新
							this.messageText = `更新成功，当前还有在进行的病例，请联系医生就诊`
							this.$refs.message.open()

							// console.log(this.registrationData);
							this.registrationData.nowPosition = 1
							this.currentUser.registrationData[0] = this.registrationData
						} else if (this.currentUser.registrationData[0].nowPosition == 2) {

							this.messageText = `该次问诊还未结束,不能进行挂号`
							this.$refs.message.open()

						} else {
							this.registrationData.nowPosition = 1
							this.currentUser.registrationData.unshift(this.registrationData)


							uni.showToast({
								title: `提交成功`
							})
						}
					} else {
						this.registrationData.nowPosition = 1
						this.currentUser.registrationData = [this.registrationData]

						uni.showToast({
							title: `提交成功`
						})
					}
					let userManagement = this.$store.state.userManagement
					userManagement = userManagement.map((item) => {
						if (item.account == this.currentUser.account) {
							item = this.currentUser
							// console.log(item);
						}
						return item
					})
					// console.log(this.currentUser);
					this.$store.commit('setUserData', userManagement)
					this.$store.commit('getLogin', this.currentUser)
				}).catch(err => {
					// console.log('err', err);
				})
			},
		},

	}
</script>

<style lang="scss">
	.personal-information {
		width: 100vw;
		opacity: 0.8;
		background: linear-gradient(white, transparent);
		padding-bottom: 80rpx;

		.uni-forms,
		.user-form {
			width: 600rpx;
			padding: 80rpx 0 10rpx 0;
			margin: 0 auto;
		}

		.btn {
			width: 500rpx;
		}

		.movetext {
			height: 100%;
			display: flex;
			align-items: center;
		}

		.movebottom {
			margin-top: 15rpx !important;
		}
	}
</style>
